package Chuong08;
import java.io.*;
import java.net.*;
public class EchoServer {
   public static void main(String[] args) {
       try {
         ServerSocket server = new ServerSocket(8010);
         while (true) {  
            Socket s = server.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                s.getInputStream()));
            PrintWriter out = new PrintWriter(new OutputStreamWriter(
                s.getOutputStream()));
            out.write("Hello! This is the Java EchoSever. ");
            out.write("Enter BYE to exit.");
            out.flush();
            String str = null;
            while ((str = in.readLine()) != null) {
               System.out.println("Received: " + str);
               out.write("Echo: " + str);
               out.flush();
               if (str.trim().equals("BYE")) {
                  break;
               }
            }
            s.close();
         }
       } catch (Exception e) { e.printStackTrace(); }
   }
}
